<?php
session_start();

$institute_id = $_SESSION['institute_id'];
$user_id= $_SESSION['user_id'];
//echo "institute_id=$institute_id<bR>";
if(isset($_POST['delete']) && $_POST['delete'] !='')
{
	//echo $_POST['del_id']."<br>";
	//exit;
	$sqltext="delete from keyword where id=?";
	$params = array();
	array_push($params,sql_escape($_POST['del_id']));
	db_change_query($conn,$sqltext,$params);	
}
if(isset($_POST['save']) && $_POST['save'] !='')
{
	$keyword = $_POST['keyword'];
	$error_message ='';
	if(!isset($keyword) ||  $keyword=='')
	{
		$error_message .= "missing required field - keyword";
	}
	if($error_message  =='')
	{
		
		$sqltext="INSERT INTO keyword(asset_id, keyword) values( ?,?)";
		$params = array();
		array_push($params,sql_escape($asset_id));
		array_push($params,sql_escape($keyword));
		db_change_query($conn,$sqltext,$params);	
	}
	else
	{
		$alert_message = $error_message;
	}
}
$keyword_area .="";
if($asset_id !='')
{
 	$asset_id= $_REQUEST['asset_id'];
	$sqltext="select keyword,id
										from keyword WHERE asset_id=?";
	$params = array();	
	array_push($params,sql_escape($asset_id));
	$result = db_select_query($conn,$sqltext,$params);
	$keyword_area .="<br /><b>List all keywords for this asset:<br/></b><br/><table border='1' cellpadding='5' cellspacing='1'><tr><th>Keyword</th><th>&nbsp;</th></tr>";
	$has_flag= false;
	while($row = db_fetch_object($result))
	{
		$has_flag= true;
		foreach ($row as $key => $value) 
		{
			$value = trim($value);
			$$key = $value;
		}	
		$keyword_area .="<tr><td>$keyword</td><td>
		<input type='submit' name='delete' value='Remove' onclick='this.form.del_id.value=".$id."' /></td></tr>";
	}
	if(!$has_flag)
	{
		$keyword_area .="<tr><td colspan='9'>No Keywords found for this asset!</td></tr>";
	}
	$keyword_area .="</table>";
	/************************start update seest check sum***************/
	//$asset_id= $_REQUEST['asset_id'];
	$sqltext="select first_name,last_name,middle_name, alt_name,suffix, department,title, organization as company,id
										from presenter WHERE asset_id=? 
										order by last_name,first_name,middle_name,suffix,alt_name,title,department,company";
	$params = array();	
	array_push($params,sql_escape($asset_id));
	$result = db_select_query($conn,$sqltext,$params);	
	unset($presenters_list);
	$presenter_checksum ='';
	while($row = db_fetch_object($result))
	{
		foreach ($row as $key => $value) 
		{
			$value = trim($value);
			$$key = $value;
		}	
		$presenters_list[] = $last_name.$first_name.$middle_name.$suffix.$alt_name.$title.$department.$company;		
	}	
	if(isset($presenters_list) )
	{
		sort($presenters_list);
		for($i=0;$i<count($presenters_list); $i++)
		{
			$presenter_checksum .= $presenters_list[$i];
		}
	}
	//*************get other informatin for asset checksum update*******/
	
	//ms array, and keyword array
	$sqltext="SELECT msc FROM msc_number  WHERE asset_id= ? order by msc";
	$params = array();
	array_push($params,sql_escape($asset_id));
	$result = db_select_query($conn,$sqltext,$params);
	unset($msc_array);	
	while($row = db_fetch_object($result))
	{
		foreach ($row as $key => $value) 
		{
			$value = trim($value);
			$msc_array[]=$value;
		}
	}
	$sqltext="SELECT keyword FROM keyword  WHERE asset_id=?  order by keyword";
	$params = array();
	array_push($params,sql_escape($asset_id));
	$result = db_select_query($conn,$sqltext,$params);
	unset($keyword_array);
	while($row = db_fetch_object($result))
	{
		foreach ($row as $key => $value) 
		{
			$value = trim($value);
			$keyword_array[]=$value;
		}
	}
	$sqltext="select 	title as asset_title, 
										abstract, 
										lecture_date,  
										duration, 
										asset_type,  
										remote_url, 
										location, 
										event_title 
										from asset WHERE id=? ";
	$params = array();	
	array_push($params,sql_escape($asset_id));
	$result = db_select_query($conn,$sqltext,$params);
	while($row = db_fetch_object($result))
	{
		
		foreach ($row as $key => $value) 
		{
			$value = trim($value);
			$$key = $value;
		}			
	}
	$lectureDate = strtotime($lecture_date);
	$msc_list ='';
	if(isset($msc_array))
	{
		for($i=0;$i<count($msc_array); $i++)
		{
			$msc_list .= $msc_array[$i];
		}
		
	}
	$keywords_list ='';
	if(isset($keyword_array))
	{
		for($i=0;$i<count($keyword_array); $i++)
		{
			$keywords_list .= $keyword_array[$i];
		}
		
	}
	$asset_string =$asset_title.$abstract.$lectureDate.$duration.$asset_type.$remote_url.$location.$event_title.$msc_list.$keywords_list.$presenter_checksum;
	//$asset_string =$asset_title.$abstract.$lectureDate.$duration.$type.$remoteURL.$location.$eventTitle.$msc_array.$keyword_array.$presenter_checksum;
	$asset_sha1 = sha1($asset_string);
//	echo $asset_string ."<br>";
	//echo $asset_sha1 ."<br>";
	$params = array();
	$sqltext="UPDATE asset SET ";		

	$sqltext .="last_modified_datetime =?,";
	array_push($params,date("Y-m-d H:i:s"));
	$sqltext .="last_modified_by =?";
	array_push($params,sql_escape($user_id));
	

	$sqltext .=",assetSHA1 =?";
	array_push($params,sql_escape($asset_sha1));

	$sqltext .=" WHERE id=?";
	array_push($params,sql_escape($asset_id));
	db_change_query($conn,$sqltext,$params);
	
	/******end**********************/
}

$keyword_area .="<input type='hidden' name='del_id' value=\"\" /><br/><input type='hidden' name='asset_id' value=\"$asset_id\" />
<b>Add new keyword:<br /></b><hr style='border-color:#999;'/><br/><table>";

$keyword_area .="
<tr><td>Keyword:</td><td> <input type='text' name='keyword' value='' size='80'  maxlength='250' /></td></tr>
";
$keyword_area .="<tr><td colspan='2' align='right'><input type='submit' name='save' value='Add Keyword'></td></tr>";
$keyword_area .="
</table>";
?>